package com.mondo.financialwallet.presentation.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.mondo.financialwallet.presentation.App;

public class LocalConfigStorage {

    private final static String FILE_NAME = "configs";
    private final static String TOKEN = "1";
    private final static String PASSWORD = "2";
    private final static String BIND_GOOGLE_VALIDATOR = "3";
    private final static String UID = "4";
    private final static String EMAIL = "5";
    private final static String MOBILE = "6";
    private final static String PREFIX = "7";

    private static LocalConfigStorage mINSTANCE;

    public static LocalConfigStorage get() {
        if (mINSTANCE == null) {
            synchronized (LocalConfigStorage.class) {
                if (mINSTANCE == null) {
                    mINSTANCE = new LocalConfigStorage();
                }
            }
        }

        return mINSTANCE;
    }

    private SharedPreferences mPreferences;

    private LocalConfigStorage() {
        mPreferences = App.getInstance().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
    }

    public void login(String token, CharSequence password, String uid, String mobile, String prefix) {
        mPreferences.edit()
                .putString(TOKEN, token)
                .putString(PASSWORD, String.valueOf(password))
                .putString(UID, uid)
                .putString(MOBILE, mobile)
                .putString(PREFIX, prefix)
                .apply();
    }

    public void setPassword(String password){
        mPreferences.edit().putString(PASSWORD, password).apply();
    }

    public String getPassword(){
        return mPreferences.getString(PASSWORD, null);
    }

    public void bindGoogleValidator(boolean state){
        mPreferences.edit().putBoolean(BIND_GOOGLE_VALIDATOR, state).apply();
    }

    public void bindEmail(boolean state){
        mPreferences.edit().putBoolean(EMAIL, state).apply();
    }

    public boolean isBindEmail(){
        return mPreferences.getBoolean(EMAIL, false);
    }

    public boolean isBindGoogleValidator(){
        return mPreferences.getBoolean(BIND_GOOGLE_VALIDATOR, false);
    }

    public boolean isLogin() {
        return !TextUtils.isEmpty(mPreferences.getString(TOKEN, null));
    }

    public String getToken() {
        return mPreferences.getString(TOKEN, "");
    }

    public String getMobile(){
        return mPreferences.getString(MOBILE, "");
    }

    public String getPrefix(){
        return mPreferences.getString(PREFIX, "");
    }

    public String getUid(){
        return mPreferences.getString(UID, "");
    }

    public void singOut() {
        mPreferences.edit()
                .remove(TOKEN)
                .remove(UID)
                .remove(PASSWORD)
                .remove(MOBILE)
                .remove(PREFIX)
                .apply();
    }
}
